<?php
namespace app\api\model;

use think\Db;
use think\Model;

class User extends Model{
    /**
     *根据账号查询用户
     */
    public function findUser($data){
        //账号编号
        if (!empty($data['name'])) {
            $where['number'] = $data['name'];
        }
        //手机号码是否注册
        if (!empty($data['phone'])){
            $where['mobile'] = $data['phone'];
        }
        //是否存在密码
        if (!empty($data['password'])){
            $where['password'] = $data['password'];
        }
        //账号是否禁用
        $where['is_lock'] = 0;
        $data = Db::name('users')->where($where)->find();
        return $data;
    }

    /**
     * 新增用户
     */
    public function insert($data){
        if (empty($data['number']) || empty($data['mobile']) || empty($data['reg_time']) || empty($data['last_login']) || empty($data['mobile_validated']) || empty($data['last_ip']) || empty($data['password'])){
            return false;
        }
        $map['number'] = $data['number'];
        $map['nickname'] = $data['number'];
        $map['mobile'] = $data['mobile'];
        $map['reg_time'] = $data['reg_time'];
        $map['last_login'] = $data['last_login'];
        $map['mobile_validated'] = $data['mobile_validated'];
        $map['last_ip'] = $data['last_ip'];
        $map['password'] = $data['password'];
        $map['head_pic'] = '/upload/pic/default.jpg';
        $map['signature'] = '他很懒，什么都没有留下';
        $result = Db::name('users')->insert($map);
        if ($result){
            return $result;
        }else{
            return false;
        }
    }

    /**
     * 用户密码修改
     */
    public function updatapwd($data){
        if (empty($data['password']) || empty($data['phone'])){
            return false;
        }
        $where['number'] = $data['phone'];
        //账号是否禁用
        $where['is_lock'] = 0;
        $map['password'] = $data['password'];
        $result = Db::name('users')->where($where)->update($map);
        return $result;
    }

    /**
     * 用户登陆日志记录-1
     */
    public function userlog($data){
        if ($data['phone']){
            $where['number'] = $data['phone'];
        }
        $where['is_lock'] = 0;
        $map['last_login'] = time();
        $map['last_ip'] = $data['ip'];
        $result = Db::name('users')->where($where)->update($map);
        return $result;
    }

    /**
     * 用户登陆日志记录-2
     */
    public function userlogB($data){
        $map['logtime'] = time();
        $map['loginfo'] = $data['type'];
        $map['ip'] = $data['ip'];
        $map['uid'] = $data['uid'];
        $result = Db::name('user_log')->insert($map);
        return $result;
    }

}